<?php
class ControllerProductManufacturer extends FrontController {
	public function index() {

		M('catalog/manufacturer');
		M('tool/image');

		$this->document->setTitle($this->_['heading_title']);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->_['text_brand'],
			'href'      => U('product/manufacturer'),
			'separator' => $this->_['text_separator']
		);

		$this->data['categories'] = array();

		$results = $this->model_catalog_manufacturer->getManufacturers();

		foreach ($results as $result) {
			if (is_numeric(utf8_substr($result['name'], 0, 1))) {
				$key = '0 - 9';
			} else {
				$key = utf8_substr(utf8_strtoupper($result['name']), 0, 1);
			}

			if (!isset($this->data['manufacturers'][$key])) {
				$this->data['categories'][$key]['name'] = $key;
			}

			$this->data['categories'][$key]['manufacturer'][] = array(
				'name' => $result['name'],
				'href' => U('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id'])
			);
		}
		
		$this->display('product/manufacturer_list.tpl');
  	}

	public function info($manufacturer_id = 0) {

		M('catalog/manufacturer');

		if (!$manufacturer_id) $manufacturer_id = $this->request->get('manufacturer_id', 0);
		$manufacturer_id = (int) $manufacturer_id;

		$manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);

		if (!$manufacturer_info) {
			return $this->forward('error/not_found');
		}

		$sort = $this->request->get('sort', 'sort_order');
		$order = $this->request->get('order', 'asc');
		$page = $this->request->get('page', 1);
		$limit = $this->request->get('limit', C('config_catalog_limit'));

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->_['text_brand'],
			'href'      => U('product/manufacturer'),
      		'separator' => $this->_['text_separator']
   		);

		$this->data['heading_title'] = $manufacturer_info['name'];

		$this->data['text_compare'] = sprintf($this->_['text_compare'], (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
	
		M('catalog/product');

		$this->document->setTitle($manufacturer_info['name']);

		$url = $this->request->get('sort,order,page,limit');

		$this->data['breadcrumbs'][] = array(
			'text'      => $manufacturer_info['name'],
			'href'      => U('product/manufacturer/info', 'manufacturer_id=' . $manufacturer_id, $url),
			'separator' => $this->_['text_separator']
		);

		$this->data['products'] = array();

		$data = array(
			'filter_manufacturer_id' => $manufacturer_id,
			'sort'                   => $sort,
			'order'                  => $order,
			'start'                  => ($page - 1) * $limit,
			'limit'                  => $limit
		);

		$product_total = $this->model_catalog_product->getTotalProducts($data);

		$results = $this->model_catalog_product->getProducts($data);

		foreach ($results as $result) {

			$this->data['products'][] = array(
				'product_id'  => $result['product_id'],
				'thumb'       => thumb($result['image'], C('config_image_product_width'), C('config_image_product_height')),
				'name'        => $result['name'],
				'summary'     => html_entity_decode($result['summary'], ENT_QUOTES, 'UTF-8'),
				'price'       => pprintf($result['price'], $result['tax_class_id']),
				'special'     => pprintf($result['special'], $result['tax_class_id']),
				'tax'         => C('config_tax') ? currency((float)$result['special'] ? $result['special'] : $result['price']) : false,
				'rating'      => C('config_review_status') ? $result['rating'] : false,
				'reviews'     => sprintf($this->_['text_reviews'], (int)$result['reviews']),
				'href'        => $result['link']
			);
		}

		//sort
		$url = "manufacturer_id=$manufacturer_id&";
		$url .= $this->request->query('limit');
		
		$this->data['sorts'] = array();

		$this->data['sorts'][] = array(
			'text'  => $this->_['text_default'],
			'value' => 'sort_order-asc',
			'href'  => U('product/manufacturer/info', $url . '&sort=sort_order&order=asc')
		);

		$this->data['sorts'][] = array(
			'text'  => $this->_['text_price_asc'],
			'value' => 'price-asc',
			'href'  => U('product/manufacturer/info', $url . '&sort=price&order=asc')
		);

		$this->data['sorts'][] = array(
			'text'  => $this->_['text_price_desc'],
			'value' => 'price-desc',
			'href'  => U('product/manufacturer/info', $url . '&sort=price&order=desc')
		);

		$this->data['sorts'][] = array(
				'text'  => $this->_['text_release_date'],
				'value' => 'date_added-desc',
				'href'  => U('product/manufacturer/info', $url . '&sort=date_added&order=desc')
		);
		
		if (C('config_review_status')) {
			$this->data['sorts'][] = array(
				'text'  => $this->_['text_rating_desc'],
				'value' => 'rating-desc',
				'href'  => U('product/manufacturer/info', $url . '&sort=rating&order=desc')
			);
			$this->data['sorts'][] = array(
				'text'  => $this->_['text_most_reviews'],
				'value' => 'reviews-desc',
				'href'  => U('product/manufacturer/info', $url . '&sort=reviews&order=desc')
			);
		}

		$url = "manufacturer_id=$manufacturer_id&";
		$url .= $this->request->query('sort,order');

		//limits
		$this->data['limits'] = array();

		$this->data['limits'][] = array(
			'text'  => C('config_catalog_limit'),
			'value' => C('config_catalog_limit'),
			'href'  => U('product/manufacturer/info', $url . '&limit=' . C('config_catalog_limit'))
		);
		
		for($i = 25; $i <= 100; $i += 25) {
			$this->data['limits'][] = array(
				'text'  => $i,
				'value' => $i,
				'href'  => U('product/manufacturer/info', $url . "&limit=$i")
			);
		}

		//pagination
		$url = "manufacturer_id=$manufacturer_id&" . $this->request->query('sort,order,limit');
		$url = U('product/manufacturer/info', $url . '&page={page}');

		$this->data['pagination'] = $this->pagination($product_total, $page, $url, $limit)->render();

		//
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		$this->data['limit'] = $limit;

		$this->display('product/manufacturer_info.tpl');
  	}
}
?>